#!/bin/sh
# PCP QA Test No. 1000
# pmdumptext segv
# https://bugzilla.redhat.com/show_bug.cgi?id=1131779
#
# Copyright (c) 2014 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

which pmdumptext >/dev/null 2>&1 || _notrun "pmdumptext not installed"
numval=`pmprobe -v mem.util.used | cut -d ' ' -f 2`
[ "$numval" -eq 1 ] || _notrun "no values for mem.util.used (numval=$numval)"

done_clean=false
_cleanup()
{
    if $done_clean
    then
	:
    else
	cd $here
	_service pcp start 2>&1 >/dev/null
	_wait_for_pmcd
	_restore_auto_restart pmcd
	_wait_for_pmlogger
	_restore_auto_restart pmlogger
	done_clean=true
    fi
    rm -rf $tmp.*
}

status=1	# failure is the default!
$sudo rm -rf $tmp.* $seq.full
trap "_cleanup; exit \$status" 0 1 2 3 15

# filter ...
# Fri Aug 22 20:04:55	7950508.000
#
_filter1()
{
    sed \
	-e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-2][0-9]:[0-5][0-9]:[0-5][0-9]/DATE/' \
	-e 's/[ 	][ 	]*[0-9][0-9]*\.[0-9][0-9]*/ NUMBER/'
}

# hostname(1)
#
_filter2()
{
    sed \
	-e "s/`hostname`/MYHOSTNAME/g" \
	-e "s/local:/MYHOSTNAME/g"
}

# real QA test starts here
echo "=== expect output ==="
pmdumptext -s 1 'localhost:mem.util.used' | _filter1

echo
echo "=== expect No route to host, no valid metrics ==="
pmdumptext -s 1 'no.such.host.pcp.io:mem.util.used'

echo
echo "=== expect No route to host, no valid metrics [instance case] ==="
pmdumptext -s 1 'no.such.host.pcp.io:proc.psinfo.utime[12345]'

echo
echo "=== expect No route to host, no valid metrics -h [instance case] ==="
pmdumptext -s 1 -h no.such.host.pcp.io 'proc.psinfo.utime[12345]'

_stop_auto_restart pmcd
_stop_auto_restart pmlogger
_service pcp stop >/dev/null 2>&1
echo
echo "=== pmcd not running, default case ==="
pmdumptext -s 1 'sample.bin[bin-300]' 2>&1 | _filter2
echo
echo "=== pmcd not running, host in metricspec case ==="
pmdumptext -s 1 'localhost:proc.psinfo.utime[12345]'
echo
echo "=== pmcd not running, -h localhost case ==="
pmdumptext -s 1 -h localhost 'proc.psinfo.utime[12345]'

# success, all done
status=0

exit
